
// const baseURL = "http://127.0.0.1:7991"

// 本地MOCK
const baseURL = "http://127.0.0.1:4523/m1/1384709-0-default"

const get = function ({url,data,successCallback,errorCallback}) {
    const pattern = /^\//
    const hasSlash = pattern.test(url)
    if(!hasSlash) {
        url = "/" + url;
    }
    
    return wx.request({
        url: baseURL + url,
        data: data,
        method: "GET",
        dataType: "json",
        success:  (res) => {
          console.log("http", res)
          if(res.statusCode == 200) {
            if(successCallback && typeof successCallback == "function") {
                successCallback(res.data);
            }
          } else {
            errorCallback(res);
          }
        },
        fail: function (err) {
            if(errorCallback && typeof errorCallback == "function") {
                errorCallback(err);
            }
        }
    })
}


const post = function ({url,data,successCallback,errorCallback}) {
  const pattern = /^\//
  const hasSlash = pattern.test(url)
  if(!hasSlash) {
      url = "/" + url;
  }
  
  return wx.request({
      url: baseURL + url,
      data: data,
      method: "POST",
      dataType: "json",
      success: function (res) {
          if(successCallback && typeof successCallback == "function") {
              successCallback(res.data);
          }
      },
      fail: function (err) {
          if(errorCallback && typeof errorCallback == "function") {
              errorCallback(err);
          }
      }
  })
}

module.exports = {
    get,
    post
}